package com.ttbj.product.api;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.jaxb.SpringDataJaxb.OrderDto;

import com.ttbj.constant.DriverEnum;
import com.ttbj.constant.GenerateNumConstants;
import com.ttbj.product.bean.dto.DriverDto;
import com.ttbj.product.bean.dto.EntitieDto;
import com.ttbj.product.bean.dto.TruckDto;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.util.DateUtils;
import com.ttbj.util.ResultUtil;
import com.ttbj.util.TokenUtils;
import com.ttbj.util.ViewModelUtil;

public interface DriverFacade {
	
	 
	/**
	 * 查询用户信息
	 * @param user
	 */
	DriverDto findDriver(DriverDto driverDto,int sen) ;
	
	 /**
	  * 添加司机信息
	  * @param driverDto
	  */
	void newDriver(DriverDto driverDto) throws Exception;
	
	/**
	 * 修改司机信息
	 * @param driverDto
	 */
	void updateDriver(DriverDto driverDto) throws Exception;
	 
	
	/**
	 * 创建货车信息
	 * @return 
	 */
	Long newTruck( Long driverId,String carNum, String truckType ) throws Exception;
	
	
	/**
	 * 模糊查询车辆信息
	 * @param carNum
	 */
	List<TruckDto> likeTruck(String carNum) throws Exception;
	
	/**
	 * 验证用户信息
	 * @param driverDto
	 */
	default boolean isEmpty(DriverDto driverDto)  throws Exception{
		if(null == findDriver(driverDto,1)){
			return true;
		}
		return false ;
	}
	
	String entityAdd(String name, String desc, String city, String district) throws Exception;
	
	/**
	 * 登陆
	 * @param driverDto
	 */
	default ViewData login(DriverDto driverDto) throws Exception{
		DriverDto driver = findDriver(driverDto,0);
		String driverToken = UUID.randomUUID().toString();
		String driverPhone = driverDto.getDriverPhone();
		if(null == driver) {	//注册
			driver = new DriverDto(TokenUtils.getNo(GenerateNumConstants.DRIVER), driverPhone, DateUtils.getCurDttStr(), driverToken, driverPhone);
			driver.setDriverFlag(DriverEnum.PHONE.getValue());
			driver.setDriverIsCompany(1);
			driver.setDriverEntity(entityAdd(driverPhone, null, null, null));
			newDriver(driver);
		}else{ //更换token
			driver.setDriverToken(driverToken);
			if(!isContainEntity(driver)){
				driver.setDriverEntity(entityAdd(driverPhone, null, null, null));
			}
			updateDriver(driver);
			driver.setDriverToken(driverToken);
		}
		return ViewModelUtil.buildSuccess("成功", driver);
	}
	
	/**
	 * 判断是否包含entity
	 * @param driver
	 * @return
	 */
	boolean isContainEntity(DriverDto driver);

	/**
	 * 用户资料补全
	 * @param driverDto
	 */
	default ViewData cmplData(DriverDto driverDto) throws Exception{
		updateDriver(driverDto);
		return ViewModelUtil.buildSuccess("成功",driverDto);
	}
	
	/**
	 * 非企业司机补全车辆信息
	 * @param driverDto
	 * @param carNum
	 * @param truckType
	 */
    default	 ViewData inputTruck(DriverDto driverDto, String carNum, String truckType) throws Exception{
        TruckDto exitDto = queryTruckDto(new TruckDto(null, carNum, null, null));
        if(null != exitDto){
        	return ViewModelUtil.buildFailure(700, "车辆信息已存在");
        }
    	Long truckId = newTruck(driverDto.getDriverId(),carNum, truckType); //生成一个货车信息
    	if(null == truckId){
    		ResultUtil.throwExcepion(ViewModelUtil.buildFailure(99999, "系统未知错误"), "添加车辆信息表出错");
    	}
    	driverDto.setDriverTruck(truckId);
    	cmplData(driverDto);
		return ViewModelUtil.buildSuccess("成功", driverDto);
    }
    
    /**
     * 匹配鹰眼标记和车辆类型
     * @param truckType
     * @param list
     * @return
     */
    List<DriverDto> matchEntityWithTruckType(Integer truckType, List<EntitieDto> list);
    
    
    /**
     * 修改车辆中的driverId
     */
    int updateTruck(TruckDto truck);
    
    /**
     * 根据车辆id查询车辆信息
     */
    TruckDto getTruck(Long truckId);

    
    /**
     * 查询司机和车辆的全部信息
     * @param orderDriver
     * @return
     */
	DriverDto queryDto(Long orderDriver);
	
	/**
	 * 根据车辆动态信息查询车辆信息
	 * @return
	 */
	TruckDto queryTruckDto(TruckDto queryDto);

	/**
	 * 登出接口
	 * @param driverDto
	 */
	void logout(DriverDto driverDto);
    
    
    
    
    
    
	
	
	

}
